<script lang="ts" setup>
import { useVbenModal } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import { putDelivery } from '#/api/plugin/pay';

const [Form, formApi] = useVbenForm({
  handleSubmit: onSubmit,
  schema: [
    {
      component: 'Input',
      dependencies: {
        show: false,
        // 随意一个字段改变时，都会触发
        triggerFields: ['id'],
      },
      fieldName: 'id',
      label: 'ID',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'name',
      label: '快递公司',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'number',
      label: '快递单号',
      rules: 'required',
    },
  ],
});
const [Modal, formModalApi] = useVbenModal({
  fullscreenButton: false,
  footer: false,
  onOpenChange(isOpen: boolean) {
    if (isOpen) {
      const values: any = formModalApi.getData();
      if (values) {
        formApi.setValues(values);
      }
    }
  },
});
async function onSubmit(values: Record<string, any>) {
  function showMsg(msg: string) {
    message.success(msg);
    formModalApi.setData({ isUpdata: true });
    formModalApi.close();
  }
  const { msg } = await putDelivery({
    id: values.id,
    express: { name: values.name, number: values.number },
  });
  showMsg(msg);
}
</script>
<template>
  <Modal title="发货"> <Form /> </Modal>
</template>

<style lang="scss" scoped></style>
